package com.zoujt.controller;

import com.zoujt.constants.Constant;
import com.zoujt.entitys.User;
import com.zoujt.service.UserService;
import com.zoujt.untils.Md5Util;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;

/*
 * @auth zoujt
 * @date 2021/2/25
 * @Description
 */
@WebServlet("/sys/login/*")
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserService();
    private User user = new User();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        user.setUsername(username);
        user.setPassword(Md5Util.md5(password));
        ArrayList<User> users = userService.checkNameAndPassword(username,password);
        //七天免登陆
        String remember =req.getParameter("remember");
//        System.out.println();
        int index = 0;
        Cookie[] cookies = req.getCookies();
        if (cookies!=null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")) {
                    if (cookie.getValue().equals(username)) {
                        index++;
                    }
                }
                if (cookie.getName().equals("password")) {
                    if (cookie.getValue().equals(password)) {
                        index++;
                    }
                }

            }
        }
        if (index == 2) {
            req.getRequestDispatcher("/success.jsp").forward(req, resp);
//                resp.sendRedirect("/success.jsp");
//            return;
        }
        if (users == null || users.size() !=1){
            resp.sendRedirect("/index.jsp");
        }else {
            HttpSession session = req.getSession();
            session.setMaxInactiveInterval(60*30);
            session.setAttribute(Constant.SESSION_LOGIN,users.get(0));
            //在线人数
            ServletContext application = getServletContext();
            int countNum = (int)application.getAttribute("countNum");
            req.setAttribute("countNum",countNum);
            req.setAttribute("username",username);
            //如果不勾选每次需要登录，则把账号密码存入cookie中，并设置过期时间
            if (!(null==remember)){
                int maxAge = Integer.valueOf(remember) * 24 * 60 * 60;
                Cookie cookieName = new Cookie("username", username);
                Cookie cookiePs = new Cookie("password", Md5Util.md5(password));
                cookieName.setPath("/");
                cookiePs.setPath("/");
                cookieName.setMaxAge(maxAge);
                cookiePs.setMaxAge(maxAge);
                resp.addCookie(cookieName);
                resp.addCookie(cookiePs);
            }

            req.getRequestDispatcher("/success.jsp").forward(req,resp);
//            resp.sendRedirect("/sys/user/list");
        }



    }
}
